// 发送请求获取统计数据
axios.get('/dashboard').then(({ data: res }) => {
    let { groupData, overview, provinceData, salaryData, year } = res.data
    // console.log(groupData, overview, provinceData, salaryData, year)
    lineChart(year)
    classSalaryChart(salaryData)
    sexSalaryChart(salaryData)
    groupSalaryChart(groupData)
    mapChart(provinceData)
    // console.log(provinceData)
    // 设置学生概况
    for (let k in overview) {
        document.querySelector(`[name=${k}]`).innerHTML = overview[k]
    }
    // document.querySelector('[name=salary]').innerHTML = overview.salary
    // document.querySelector('[name=student_count]').innerHTML = overview.student_count
    // document.querySelector('[name=age]').innerHTML = overview.age
    // document.querySelector('[name=group_count]').innerHTML = overview.group_count
})



// 2022全学科薪资走势
function lineChart(data) {
    const myChart = echarts.init(document.querySelector('#line'))
    const option = {
        title: {
            text: '2022全学科薪资走势',
            top: 15,
            left: 10,
            textStyle: {
                fontSize: 16
            }
        },
        xAxis: {
            type: 'category',
            data: data.map(item => item.month),
            axisLabel: {
                color: '#999'
            },
            axisLine: {
                lineStyle: {
                    color: '#ccc',
                    type: 'dashed'
                }
            }
        },
        yAxis: {
            type: 'value',
            splitLine: {
                lineStyle: {
                    type: 'dashed'
                }
            }
        },
        tooltip: {
            trigger: 'axis' //鼠标放到轴上就会触发提示
        },
        grid: {
            top: '20%'
        },
        series: [
            {
                data: data.map(item => item.salary),
                type: 'line',
                smooth: true,
                lineStyle: {
                    width: 6 //线条粗细
                },
                symbolSize: 10, //拐点大小
                areaStyle: { //面积区域 折线和x轴围成的区域
                    color: {
                        type: 'linear',
                        x: 0,
                        y: 0,
                        x2: 0,
                        y2: 1,
                        colorStops: [
                            {
                                offset: 0,
                                color: '#499FEE'
                            },
                            {
                                offset: 0.8,
                                color: 'rgba(255,255,255,0.2)'
                            },
                            {
                                offset: 1,
                                color: 'rgba(255,255,255,0)'
                            }
                        ],
                        global: false
                    }
                }
            }
        ],
        color: {
            type: 'linear',
            x: 0,//范围0~1 折线组成的区域
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [ //颜色位置
                {
                    offset: 0,  //起点
                    color: '#499FEE'
                },
                {
                    offset: 1, //终点
                    color: '#5D75F0'
                }
            ],
            global: false
        }
    }
    myChart.setOption(option)
}

// 班级薪资分布
function classSalaryChart(data) {
    const myChart = echarts.init(document.querySelector('#salary'))
    const option = {
        title: {
            text: '班级薪资分布',
            top: 15,
            left: 10,
            textStyle: {
                fontSize: 16
            }
        },
        tooltip: {
            trigger: 'item' //鼠标移入每一项中提示
        },
        legend: {
            bottom: '6%',
            left: 'center'
        },
        series: [
            {
                name: '班级薪资分布',
                type: 'pie',
                radius: ['50%', '64%'],//内外圈半径
                // center:[x,y],中心点坐标
                itemStyle: {
                    borderRadius: 10,
                    borderColor: '#fff',
                    borderWidth: 2
                },
                label: {
                    show: false, //默认ture
                    position: 'center'
                },
                labelLine: {
                    show: false //默认ture
                },
                data: data.map(item => {
                    return {
                        value: item.g_count + item.b_count,
                        name: item.label
                    }
                })
            }
        ],
        color: ['#FDA224', '#5097FF', '#3ABCFA', '#34D39A']
    };
    myChart.setOption(option)
}

// 男女薪资分布
function sexSalaryChart(data) {
    const myChart = echarts.init(document.querySelector('#gender'))
    const option = {
        title: [ //设置多个标题使用数组形式
            {
                text: '男女薪资分布',
                top: 10,
                left: 10,
                textStyle: {
                    fontSize: 16
                }
            },
            {
                text: '男生',
                top: '45%',
                left: '45%',
                textStyle: {
                    fontSize: 16
                }
            },
            {
                text: '女生',
                top: '80%',
                left: '45%',
                textStyle: {
                    fontSize: 16
                }
            },
        ],
        tooltip: {
            trigger: 'item' //鼠标移入每一项中提示
        },
        legend: {
            bottom: '6%',
            left: 'center'
        },
        series: [
            {
                name: '男生薪资分布',
                type: 'pie',
                radius: ['20%', '30%'],
                center: ['50%', '30%'],
                data: data.map(item => ({ name: item.label, value: item.b_count }))
            },
            {
                name: '女生薪资分布',
                type: 'pie',
                radius: ['20%', '30%'],
                center: ['50%', '65%'],
                data: data.map(item => ({ name: item.label, value: item.g_count }))
            }
        ],
        color: ['#FDA224', '#5097FF', '#3ABCFA', '#34D39A']
    };
    myChart.setOption(option)
}

// 每组薪资
function groupSalaryChart(data) {
    const myChart = echarts.init(document.querySelector('#lines'))
    const option = {
        xAxis: {
            type: 'category',
            data: data[1].map(item => item.name),
            axisLabel: {
                color: '#999'
            },
            axisLine: {
                lineStyle: {
                    color: '#ccc',
                    type: 'dashed'
                }
            }
        },
        yAxis: {
            type: 'value',
            splitLine: {
                lineStyle: {
                    type: 'dashed'
                }
            }
        },
        tooltip: {
        },
        grid: {
            left: 70,
            top: 30,
            right: 30,
            bottom: 50,
        },
        series: [
            {
                data: data[1].map(item => item.hope_salary),
                type: 'bar',
                name: '期望薪资' // 这个数据的名字，可以在鼠标移入的提示上显示
            },
            {
                data: data[1].map(item => item.salary),
                type: 'bar',
                name: '就业薪资' // 这个数据的名字，可以在鼠标移入的提示上显示
            }
        ],
        color: [
            {
                type: 'linear',
                x: 0,//范围0~1 折线组成的区域
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [ //颜色位置
                    {
                        offset: 0,  //起点
                        color: '#34D39A'
                    },
                    {
                        offset: 1, //终点
                        color: 'rgba(52,211,154,0.2)'
                    }
                ],
                global: false
            },
            {
                type: 'linear',
                x: 0,//范围0~1 折线组成的区域
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [ //颜色位置
                    {
                        offset: 0,  //起点
                        color: '#499FEE'
                    },
                    {
                        offset: 1, //终点
                        color: 'rgba(73,159,238,0.2)'
                    }
                ],
                global: false
            }
        ]
    }
    myChart.setOption(option)
    //切换分组功能
    document.querySelector('#btns').addEventListener('click', function (e) {
        if (e.target.nodeName === 'BUTTON') {
            document.querySelector('#btns .btn-blue').classList.remove('btn-blue')
            e.target.classList.add('btn-blue')
            option.xAxis.data = data[e.target.innerHTML].map(item => item.name)
            option.series[0].data = data[e.target.innerHTML].map(item => item.hope_salary)
            option.series[1].data = data[e.target.innerHTML].map(item => item.salary)
            myChart.setOption(option)
        }
    })
}

// 籍贯分布
function mapChart(data) {
    // 让默认数据都为0
    const mapData = [
        { name: '南海诸岛', value: 0 },
        { name: '北京', value: 0 },
        { name: '天津', value: 0 },
        { name: '上海', value: 0 },
        { name: '重庆', value: 0 },
        { name: '河北', value: 0 },
        { name: '河南', value: 0 },
        { name: '云南', value: 0 },
        { name: '辽宁', value: 0 },
        { name: '黑龙江', value: 0 },
        { name: '湖南', value: 0 },
        { name: '安徽', value: 0 },
        { name: '山东', value: 0 },
        { name: '新疆', value: 0 },
        { name: '江苏', value: 0 },
        { name: '浙江', value: 0 },
        { name: '江西', value: 0 },
        { name: '湖北', value: 0 },
        { name: '广西', value: 0 },
        { name: '甘肃', value: 0 },
        { name: '山西', value: 0 },
        { name: '内蒙古', value: 0 },
        { name: '陕西', value: 0 },
        { name: '吉林', value: 0 },
        { name: '福建', value: 0 },
        { name: '贵州', value: 0 },
        { name: '广东', value: 0 },
        { name: '青海', value: 0 },
        { name: '西藏', value: 0 },
        { name: '四川', value: 0 },
        { name: '宁夏', value: 0 },
        { name: '海南', value: 0 },
        { name: '台湾', value: 0 },
        { name: '香港', value: 0 },
        { name: '澳门', value: 0 }
    ]
    mapData.forEach(item => {
        let res = data.find(v => {
            return v.name.includes(item.name)
        })
        if (res) {
            item.value = res.value
        }
    })
    // console.log(mapData)
    const myChart = echarts.init(document.querySelector('#map'))
    const option = {
        title: {
            text: '籍贯分布',
            top: 10,
            left: 10,
            textStyle: {
                fontSize: 16
            }
        },
        series: [
            {
                type: 'map',
                name: '籍贯分布',
                map: 'china',
                itemStyle: {
                    borderColor: 'rgba(0, 0, 0, 0.2)', //分界线颜色
                    areaColor: '#E0FFFF' //区域颜色
                },
                label: {
                    show: true,
                    fontSize: 10,
                    color: 'rgba(0, 0, 0, 0.7)'
                },
                emphasis: {// 高亮状态下样式
                    itemStyle: {
                        areaColor: '#34D39A',
                        borderWidth: 0,
                        shadowBlur: 20,
                        shadowOffsetX: 0,
                        shadowOffsetY: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                },
                labelLine: {
                    show: false
                },
                data: mapData
            },
        ],
        tooltip: {
            formatter: '{b}: {c}位学员',//自定义提示
            backgroundColor: 'rgba(0, 0, 0, 0.5)',
            borderColor: 'transparent',
            textStyle: {
                color: '#fff'
            }
        },
        visualMap: {//视觉映射
            type: 'continuous', // continuous连续型 piecewise分段式
            max: 20,
            min: 0,
            text: [20, 0],
            left: 40,
            bottom: 20,
            inRange: { color: ['#fff', '#0075F0'] }
        }
    }
    myChart.setOption(option)
}